import {defineStore} from "pinia";
import {wirteReprotType} from "@/store/modules/dataWriteReport/type";
import {
    addData,
    deleteData,
    getLookTableData,
    getLookTableHeadrs,
    getWriteReportList,
    inputDataValue
} from "@/api/datWriteReport";
import {inputDataParamsType, lookTableDataParamsType, writeReportListParamsype} from "@/api/datWriteReport/type";


let useDataWriteReportStore = defineStore('dataWriteReport', {
    state: (): wirteReprotType => {
        return {
            dataName: '',

            tableLoading: false,
            table: {
                tableData: [],
                tableHeader: [
                    {
                        name: '数据名称',
                        prop: 'dataName'
                    },
                    {
                        name: '数据标识',
                        prop: 'dataKey'
                    },
                    {
                        name: '最新填报日期',
                        prop: 'lastInputTime'
                    },
                ],
                page: 1,
                limit: 10,
                total: 0,
            },
            lookTable: {
                lookTableData: [],
                lookTableHeader: [],
                page: 1,
                limit: 10,
                total: 0,
            },
            addParams: {
                createTime: '',
                dataKey: '',
                dataName: '',
                deleted: '',
                fieldList: [],
                id: '',
                lastInputTime: '',
                remark: '',
            },
            inputDataParams:{
                dataId:'',
                dataTime:'',
                fieldList:[]
            }
        }
    },
    actions: {
        async useGetWriteReportList() {
            this.tableLoading = true
            try {
                let params: writeReportListParamsype = {
                    page: this.table.page,
                    limit: this.table.limit,
                    dataName: this.dataName
                }
                let res = await getWriteReportList(params)
                this.table.tableData = res.data.list
                this.table.total = res.data.list.length
                this.tableLoading = false
            } catch (err) {
                this.tableLoading = false
            }

        },
        useAddData() {
            return addData(this.addParams)
        },
        async useGetLookTableHeadrs(params: { dataId: string | number }) {
            let res = await getLookTableHeadrs(params)
            this.lookTable.lookTableHeader = res.data
        },
        async useGetLookTableData(param: { dataId: string, dataTime: string }) {
            this.tableLoading = true
            try {
                let params: lookTableDataParamsType = {
                    ...param,
                    page: this.lookTable.page,
                    limit: this.lookTable.limit
                }
                let res = await getLookTableData(params)
                this.lookTable.lookTableData = res.data.list
                this.lookTable.total = res.data.total
                this.tableLoading = false
            } catch (err) {
                this.tableLoading = false
            }
        },
        useDeleteData(dataId: string) {
            return deleteData({dataId: dataId})
        },
        useInputDataValue(){
           return  inputDataValue(this.inputDataParams)
        }

    },
    getters: {}
})

export default useDataWriteReportStore
